#!/usr/bin/env python
# -*- coding:utf-8 -*- 
import sqlite3

class sqliteDB:
    def __init__(self,dbpath):
        self.con = sqlite3.connect(dbpath)
        self.cur = self.con.cursor()
    
    def __del__(self): 
        self.cur.close()
        self.con.close()
        
    #(Stuid TEXT, Age INTERGER, Name TEXT)
    def create_table(self,tabledata,collection):
        if collection == None or tabledata == None:
            return False
        strSQL = 'CREATE TABLE {0}{1}'.format(collection,tabledata)
        self.cur.execute(strSQL)
        self.commit()

    def commit(self):
        self.con.commit()

    def update(self,str_opc):
        if str_opc == None:
            return False
        strSQL = str_opc
        self.cur.execute(strSQL)
        self.commit()
        
    def insert(self,buf,collection):
        if buf == None or collection == None:
            return False
        strSQL = 'INSERT INTO {0} VALUES (\'{1}\')'.format(collection,buf)
        #print strSQL
        self.cur.execute(strSQL)
        self.commit()
    
    def findall(self,table_name,condition):
        if condition == "":
            self.cur.execute('SELECT * FROM {} '.format(table_name))
        else:
            self.cur.execute('SELECT * FROM {} WHERE {}'.format(table_name,condition))
        dat = self.cur.fetchall()
        return dat
        # print(self.cur.fetchone())  #从结果中取一条记录，并将游标指向下一条记录
        # print(self.cur.fetchall())

if __name__ == '__main__':
    db = sqliteDB("downdb.db")
    #db.create_table("(Stuid TEXT, Age INTERGER, Name TEXT)","Student")
    #db.insert("(\"00001\", 20, \"Lucy\")","Student")
    a = ('ss',20)
    res =  db.findall("spider_download_history","")
    print (res)
    import json
    #print res[1][0]
    json_data =  json.loads(res[1][0],encoding="utf-8")
    print (json_data["article_name"].encode("utf-8"))
    #db.insert("22","spider_download_history")
    
    